﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sac.Web;

public partial class Admin_RootAndPowerManage : System.Web.UI.Page
{
    RolePowerOperate role = new RolePowerOperate();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gridRole.DataSource = role.GetAllRole();
            gridRole.DataBind();
        }
    }

    protected void btnSure_Click(object sender, EventArgs e)
    {
        string roleName = tbRootName.Text.Trim(), message = ""; 
        string roleId = lblRoleId.Text;
        lblRoleId.Text = "";
        tbRootName.Text = "";
        try
        {
            if (roleId == "")
            {
                if (role.InsertRole(roleName))
                {
                    gridRole.DataSource = role.GetAllRole();
                    gridRole.DataBind();
                    message = "添加角色名成功！";
                }
                else
                {
                    message = "添加角色名失败！请检查是否存在相同的角色名！";
                }
            }
            else
            {
               
                if (role.UpdateRole(roleId, roleName))
                {
                    gridRole.DataSource = role.GetAllRole();
                    gridRole.DataBind();
                    message = "修改角色名成功！";
                }
                else
                {
                    message = "修改角色名失败！请检查是否存在相同的角色名！";
                }
            }
        }
        catch(Exception ex)
        {
            message = "添加角色名失败！失败信息：" + ex.Message.ToString();
        }
        
        Response.Write("<script>alert('" + message + "')</script>");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        lblRoleId.Text = "";
        tbRootName.Text = "";
    }

    protected void gridRole_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string roleId = gridRole.DataKeys[e.NewSelectedIndex].Value.ToString();
        RolePower roleInfo = role.GetRoleById(roleId);
        lblRoleId.Text = roleInfo.IdKey.ToString();
        tbRootName.Text = roleInfo.RoleName;
    }

    protected void gridRole_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string roleId = gridRole.DataKeys[e.RowIndex].Value.ToString();
        string message = "";
        if (role.DeleteRoleById(roleId))
        {
            gridRole.DataSource = role.GetAllRole();
            gridRole.DataBind();
            message = "删除角色成功！";
        }
        else
        {
            message = "删除角色失败，请检查改角色下是否有用户！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
    }
}
